package com.elecbook.content.api.controller;

import com.elecbook.base.dto.PageDTO;
import com.elecbook.base.result.Result;
import com.elecbook.content.model.dto.CommentPQuery;
import com.elecbook.content.model.dto.CommentSubmitDTO;
import com.elecbook.content.model.dto.CommentUpdateDTO;
import com.elecbook.content.model.dto.CommnetPageDTO;
import com.elecbook.content.model.po.Comment;
import com.elecbook.content.model.vo.CommentVO;
import com.elecbook.content.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @PostMapping("/submit")
    public Result<CommentVO> submitComment(@RequestBody CommentSubmitDTO dto) {
        CommentVO commentVO = commentService.submitComment(dto);
        return Result.success(commentVO);
    }


    @PostMapping("/page")
    public Result<PageDTO<CommentVO>> page(@RequestBody CommnetPageDTO pageDTO) {
        PageDTO<CommentVO> pageResult = commentService.queryCommentPage(pageDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/updata")
    public Result<Comment> update(@RequestBody CommentUpdateDTO dto) {
         Comment comment= commentService.updateByDto(dto);
        return Result.success(comment);
    }

    @PostMapping("/delete")
    public Result<String > delete(@RequestBody CommentUpdateDTO dto) {
        commentService.deleteByDto(dto);
        return Result.success();
    }

    /*
    管理员生成方法
     */

    @PostMapping("/op/save")
    Result<Comment> save(@RequestBody Comment comment) {
        Comment res = commentService.saveComment(comment);
        return Result.success(res);
    }

    @DeleteMapping("/op/{id}")
    Result<String> delete(@PathVariable Long id) {
        commentService.removeById(id);
        return Result.success();
    }

    @GetMapping("/op/{id}")
    Result<Comment> get(@PathVariable Long id) {
        Comment res =  commentService.getById(id);
        return Result.success(res);
    }

    @PutMapping("/op/update")
    Result<Comment> update(@RequestBody Comment comment) {
        Comment res =  commentService.updateCommentById(comment);
        return Result.success(res);
    }

    @PostMapping("/op/page")
    Result<PageDTO<Comment>> page(@RequestBody CommentPQuery commentQuery) {
        PageDTO<Comment> pageResult = commentService.pageQuery(commentQuery);
        return Result.success(pageResult);
    }


}
